package com.xuecheng.media.controller;

import com.xuecheng.api.learning.model.vo.ResourceUrlModel;
import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.model.aliyun.VodUploadRequest;
import com.xuecheng.api.media.model.aliyun.VodUploadToken;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.media.common.utils.UAASecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.media.service.MediaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping
public class MediaController implements MediaApi {

    @Autowired
    private MediaService  mediaService;

    @PostMapping("media/vod-token")
    public VodUploadToken createVODUploadToken(@RequestBody VodUploadRequest request) {

        return mediaService.createVODUploadToken(request);
    }

    @GetMapping("media/refresh-vod-token/{videoId}")
    public VodUploadToken refreshVODUploadToken(@PathVariable String videoId) {
        return mediaService.refreshVODUploadToken(videoId);
    }

    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO vo) {
        //vo-->dto
        MediaDTO mediaDTO = MediaConvert.INSTANCE.vo2dto(vo);
        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        mediaDTO.setCompanyId(companyId);

        return mediaService.createMedia(mediaDTO);
    }

    @PostMapping("media/list")
    public PageVO queryMediaList(PageRequestParams params, @RequestBody QueryMediaModel model) {
        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        if (ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        return mediaService.queryMediaList(params, model, companyId);
    }

    @GetMapping("media/preview/{mediaId}")
    public Object previewMedia(@PathVariable Long mediaId) {

        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        if (ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        return mediaService.previewMedia(mediaId, companyId);
    }

    @DeleteMapping("media/{mediaId}")
    public void removeMedia(@PathVariable Long mediaId) {

        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        //调用service
        mediaService.removeMedia(mediaId, companyId);
    }

    @GetMapping("l/media/{mediaId}")
    public RestResponse<MediaDTO> getMediaById4s(@PathVariable Long mediaId) {

        return mediaService.getMediaById4s(mediaId);
    }

    @GetMapping("l/media-url/{mediaId}")
    public RestResponse<ResourceUrlModel> getMediaUrl(@PathVariable Long mediaId) {

        return mediaService.getMediaUrl(mediaId);
    }

}
